import {Injectable} from '@angular/core';
import {HttpClient} from '@angular/common/http';
import {Subject} from 'rxjs';
import {saveAs} from 'file-saver';
import {map} from "rxjs/operators";

@Injectable()
export class HumanService {
    /**用于在路由的环境下与父组件通讯 */
    private routeMsgPushSource = new Subject<any>();
    routeMsgPush$ = this.routeMsgPushSource.asObservable();

    constructor(private http: HttpClient) {
    }

    /** 初始化人员相关字典项目 */
    initDictInfo(sysId: number) {
        return this.http.get('/system/api/humaninfo/dict?sysId=' + sysId).pipe(
            map((res: Response) => res['data'])
        )
    }

    /** 获取系统树状结构 */
    getUnitHumanTree(unitId: number, isRoot: Boolean, isSupervise: string = "") {
        return this.http.get('/system/api/humaninfo/listtree?unitId=' + unitId + "&isRoot=" + isRoot + "&isSupervise=" + isSupervise).pipe(
            map((res: Response) => res['data'])
        );
    }

    /** 获取系统树状结构 */
    getSimpleUnitHumanTree(unitId: number, isRoot: Boolean) {
        return this.http.get('/system/api/humaninfo/simplelisttree?unitId=' + unitId + "&isRoot=" + isRoot).pipe(
            map((res: Response) => res['data'])
        );
    }

    /** 根据机构加载人员信息 */
    loadHumansByUnitId(unitId: number, isSupervise: string = "") {
        return this.http.get('/system/api/humaninfo/loadhumanbyunitid?id=' + unitId + '&isSupervise=' + isSupervise).pipe(
            map((res: Response) => res['data'])
        );
    }

    /** 根据机构加载人员信息 */
    loadSimpleHumansByUnitId(unitId: number) {
        return this.http.get('/system/api/humaninfo/loadsimplehumanbyunitid?id=' + unitId).pipe(
            map((res: Response) => res['data'])
        );
    }

    /** 加载机构人员 */
    loadhumans(unitId: number){
        return this.http.get('/system/api/humaninfo/loadhumans?unitId=' + unitId).pipe(
            map((res: Response) => res['data'])
        );
    }

    /** 查询人员姓名 */
    searchUnitHumanByName(humanName: string) {
        return this.http.get('/system/api/humaninfo/search?name=' + humanName).pipe(
            map((res: Response) => res['data'])
        );
    }

    /** 加载人员信息 */
    loadHumanById(human: any) {
        let url = (human.humanId ? '/system/api/humaninfo/load?id=' + human.humanId : '/system/api/humaninfo/showadd?unitId=' + human.unitId);
        return this.http.get(url).pipe(
            map((res: Response) => res['data'])
        )
    }

    /** 保存更新人员资源属性 */
    saveUpdateHumanResources(humanResources: any) {
        return this.http.post('/system/api/humaninfo/saveupdatehumanresources', humanResources).pipe(
            map((res: Response) => res['data'])
        );
    }

    /** 加载人员资源权限  */
    loadHumanResources(humanId: number) {
        return this.http.get('/system/api/humaninfo/loadhumanresources?humanId=' + humanId).pipe(
            map((res: Response) => res['data'])
        );
    }

    /** 加载人员岗位信息 */
    getRolesByHuman(humanId: number) {
        return this.http.get('/system/api/humaninfo/loadrolesbyhuman?id=' + humanId).pipe(
            map((res: Response) => res['data'])
        );
    }

    /** 删除人员信息 */
    delete(humanId: number) {
        return this.http.get('/system/api/humaninfo/delete?id=' + humanId).pipe(
            map((res: Response) => res['data'])
        );
    }

    /** 保存或新增人员信息 */
    saveUpdateHuman(humanInfo: any) {
        return this.http.post('/system/api/humaninfo/saveupdate', humanInfo).pipe(
            map((res: Response) => res['data'])
        )
    }

    /** 保存或更新人员岗位信息 */
    saveUpdateHumanRoles(roleHumans: any) {
        return this.http.post('/system/api/humaninfo/saveupdatehumanroles', roleHumans).pipe(
            map((res: Response) => res['data'])
        )
    }

    /** 获取岗位下的人员信息 */
    getHumansByUnit(unitId: number) {
        return this.http.get('/system/api/humaninfo/loadhumanbyunitid?id=' + unitId).pipe(
            map((res: Response) => res['data'])
        )
    }

    /** 对岗位下的人员信息进行重排序 */
    reorderHuman(humans: any) {
        return this.http.post('/system/api/humaninfo/reorderhumans', humans).pipe(
            map((res: Response) => res['data'])
        )
    }

    /** 人员导入模板下载 */
    downloadTemplate(fileName) {
        return this.http.get('/system/api/humaninfo/template', {'responseType': 'blob'}).pipe(
            map(res => saveAs(res, fileName)))
    }

    /**下载导入模板中的错误数据 */
    downloadErrorData(fileName, units) {
        return this.http.post('/system/api/humaninfo/downloaderror', units, {'responseType': 'blob'}).pipe(
            map(res => saveAs(res, fileName)))
    }

    /**数据导入 */
    importData(units: any, parentUnitId: number) {
        return this.http.post('/system/api/humaninfo/import?unitId=' + parentUnitId, units).pipe(
            map((res: Response) => res['data'])
        )
    }

    /**父子组件通讯 */
    announceMission(res: any) {
        this.routeMsgPushSource.next(res);
    }
}
